<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ xAxisCol, config, data, type, formater4Tooltip } = {}) {
      if (!config) { return }
      var legendData = config.map(function(json) {
        return json.colName
      })
      var xAxisData = data.map(function(json) {
        return json[xAxisCol]
      })
      var seriesData = config.map(function(json) {
        return {
          name: json.colName,
          type: type || 'line',
          data: data.map(function(datajson) {
            return datajson[json.col]
          }),
          stack: json.stack || '',
          smooth: true,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          markPoint: {
            data: [
              { type: 'max', name: '最大值' }
            ]
          },
          markLine: {
            data: [
              { type: 'average', name: '平均值' }
            ]
          }
        }
      })

      this.chart.setOption({
        xAxis: {
          data: xAxisData,
          boundaryGap: type === 'bar',
          axisTick: {
            show: false
          }
        },
        grid: {
          top: '12%',
          left: '1%',
          right: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          formatter: formater4Tooltip || null
        },
        dataZoom: {
          orient: 'horizontal', // 水平显示
          show: true, // 显示滚动条
          start: xAxisData.length > 10 ? 20 : 0, // 起始值为0%
          end: xAxisData.length > 10 ? 80 : 100 // 结束值为100%
        },

        toolbox: {
          // y: 'bottom',
          feature: {
            magicType: {
              type: ['line', 'bar', 'stack', 'tiled']
            },
            dataView: {
              show: false

            },
            saveAsImage: {
              pixelRatio: 2
            }

          },
          right: '1%'
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: legendData
        },
        series: seriesData
      }, true)
    }
  }
}
</script>
